//
//  HttpRequestManager.m
//  ShopXinyi
//
//  Created by qianfeng on 15/9/15.
//  Copyright (c) 2015年 qianfeng. All rights reserved.
//

#import "HttpRequestManager.h"

#define HOMEURL @"http://shop.xinyi.com/mobile/?act=index&op=miaosha"
#define CLASSURL @"http://shop.xinyi.com/mobile/index.php?act=goods_class"
#define DISCOUNTURL @"http://shop.xinyi.com/mobile/?act=special&op=special&special_id=16"
#define CARTURL @"http://shop.xinyi.com/mobile/index.php?act=member_cart&op=cart_list"
#define LOGINURL @"http://shop.xinyi.com/mobile/index.php?act=login&p=m"
#define USERURL @"http://shop.xinyi.com/mobile/index.php?act=member_index"
#define DETAILURL @"http://shop.xinyi.com/mobile/index.php?act=goods&op=goods_detail&goods_id=%@"


@implementation HttpRequestManager



//网络请求类单例
+(instancetype)shareInstance
{
    static HttpRequestManager *manager = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken,^{
        manager = [HttpRequestManager new];
    });
    return manager;
}





-(void)getHomePageInfoSuccess:(httpSuccess)success failure:(httpFailure)failure
{
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    [manager GET:HOMEURL parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSDictionary *dic = responseObject;
        success(dic[@"datas"]);
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        failure(error);
    }];
}


-(void)getClassPageInfoSuccess:(httpSuccess)success failure:(httpFailure)failure
{
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    [manager GET:CLASSURL parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSDictionary *dic = responseObject;
        NSDictionary *dic2 = dic[@"datas"];
        NSArray *array = dic2[@"class_list"];
        success(array);
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        failure(error);
    }];
}

-(void)getClassPageInfoWithID:(NSString *)goodClassID Success:(httpSuccess)success failure:(httpFailure)failure
{
    NSString *urlString = [CLASSURL stringByAppendingFormat:@"&gc_id=%@",goodClassID];
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    [manager GET:urlString parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSArray *array = responseObject;
        success(array);
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        failure(error);
    }];
}

//分类获取图片
-(void)getClassPictureWithGoodsID:(NSString *)goodsID success:(httpSuccess)success failure:(httpFailure)failure
{
    NSString *urlString = [NSString stringWithFormat:@"http://shop.xinyi.com/mobile/index.php?act=goods&op=goods_list&curpage=1&gc_id=%@&page=10 ",goodsID];
    urlString = [urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    [manager GET:urlString parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSDictionary *dic = responseObject;
        NSDictionary *dataDic =  dic[@"datas"];
        NSArray *goodsList = dataDic[@"goods_list"];
        NSDictionary *goodDic = [goodsList firstObject];
        NSString *imageURL = goodDic[@"goods_image_url"];
        success(imageURL);
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        failure(error);
    }];
}


-(void)getDiscountGoodsInfoSuccess:(httpSuccess)success failure:(httpFailure)failure
{
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    [manager GET:DISCOUNTURL parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSDictionary *dic = responseObject;
        NSDictionary *dataDic =  dic[@"datas"];
        success(dataDic);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        failure(error);
    }];

}

-(void)postCartInfoWithKey:(NSString *)key success:(httpSuccess)suceess failure:(httpFailure)failure
{
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    [manager POST:CARTURL parameters:@{@"key":key} success:^(AFHTTPRequestOperation *operation, id responseObject) {
        suceess(responseObject);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        failure(error);
    }];
}

-(void)loginWithClient:(NSString *)client andPassword:(NSString *)password andUsername:(NSString *)username sueecss:(httpSuccess)success failure:(httpFailure)failure
{
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    [manager POST:LOGINURL parameters:@{@"client":client,@"password":password,@"username":username} success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSDictionary *dic = responseObject;
        NSDictionary *datasDic = dic[@"datas"];
        success(datasDic);
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        failure(error);
    }];
    
}

-(void)getUserInfoWithKey:(NSString *)key success:(httpSuccess)success failure:(httpFailure)failure
{
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    [manager POST:USERURL parameters:@{@"key":key} success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSDictionary *dic = responseObject;
        NSDictionary *datasDic = dic[@"datas"];
        NSDictionary *infoDic = datasDic[@"member_info"];
        success(infoDic);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        failure(error);
    }];
}

//详情页
-(void)getDetailInfoWithGoodsID:(NSString *)goodsID success:(httpSuccess)success failure:(httpFailure)failure
{
    NSString *urlString = [NSString stringWithFormat:DETAILURL,goodsID];
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    [manager GET:urlString parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSDictionary *dic = responseObject;
        NSDictionary *datasDic = dic[@"datas"];
        success(datasDic);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        failure(error);
    }];
    
}

@end
